package tf2pickup.listeners;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Set;
import org.pircbotx.PircBotX;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;
import org.pircbotx.hooks.events.OpEvent;
import tf2pickup.Players;
import tf2pickup.Tf2Class;

/**
 *
 * @author Philipp Preß
 */
public class PrintStatus extends ListenerAdapter<PircBotX> {

    private Players players;
    private HashMap<String, Integer> maps;

    public PrintStatus(Players players, HashMap<String, Integer> maps) {
        this.players = players;
        this.maps = maps;
    }

    /*
     * Refreshes the topic with the pickup-status when command !status has been
     * sent.
     */
    @Override
    public void onMessage(MessageEvent<PircBotX> event) {
        if (event.getMessage().equalsIgnoreCase("!status")) {
            event.getBot().setTopic(event.getChannel(), this.getStatus());
        }
    }

    /**
     * 
     * Refreshes the topic with the pickup-status when the bot becomes OP in a 
     * channel.
     */
    @Override
    public void onOp(OpEvent<PircBotX> event) {
        if (event.getRecipient().getNick().equals(event.getBot().getNick())) {
            event.getBot().setTopic(event.getChannel(), this.getStatus());
        }
    }

    /**
     * 
     * @return a well-formated status-line, containing current players and map-votes 
     */
    public String getStatus() {
        EnumMap<Tf2Class, Set<String>> playersByTf2Class;
        StringBuffer output;
        int totalMapVotes;

        playersByTf2Class = new EnumMap<Tf2Class, Set<String>>(Tf2Class.class);
        output = new StringBuffer();
        totalMapVotes = 0;

        playersByTf2Class.put(Tf2Class.MEDIC, players.getPlayersByTf2Class(Tf2Class.MEDIC));
        playersByTf2Class.put(Tf2Class.DEMOMAN, players.getPlayersByTf2Class(Tf2Class.DEMOMAN));
        playersByTf2Class.put(Tf2Class.SOLDIER, players.getPlayersByTf2Class(Tf2Class.SOLDIER));
        playersByTf2Class.put(Tf2Class.SCOUT, players.getPlayersByTf2Class(Tf2Class.SCOUT));


        // 
        output.append(playersByTf2Class.get(Tf2Class.DEMOMAN).size()
                + playersByTf2Class.get(Tf2Class.SOLDIER).size()
                + playersByTf2Class.get(Tf2Class.SCOUT).size());
        output.append(" + ").append(playersByTf2Class.get(Tf2Class.MEDIC).size()).append(": ");

        //Line-up
        //Example: DEMOMAN(Player 1, Player 2, Player 3, x)
        for (Tf2Class tf2Class : playersByTf2Class.keySet()) {
            output.append(tf2Class).append("(");
            for (String nickname : playersByTf2Class.get(tf2Class)) {
                output.append(nickname).append(", ");
            }
            //If the classlimit isn't reached, we not to fill the empty spots with 'x's.
            if (playersByTf2Class.get(tf2Class).size() < (tf2Class.getClassLimit() * 2)) {
                for (int i = 0; i < (tf2Class.getClassLimit() * 2) - playersByTf2Class.get(tf2Class).size(); i++) {
                    output.append("x, ");
                }
            }
            output.setLength(output.length() - 2);
            output.append(") ");
        }
        output.setLength(output.length() - 1);
        output.append(". ");

        //Maps
        output.append("Maps: ");
        for (int votes : maps.values()) {
            totalMapVotes += votes;
        }
        if (totalMapVotes == 0) {
            output.append("none.");
            return output.toString();
        }
        for (String map : maps.keySet()) {
            if (maps.get(map) > 0) {
                output.append(map).append('(').append(maps.get(map)).append(')');
                output.append(", ");
            }
        }
        output.setLength(output.length() - 2);
        output.append(".");
        return output.toString();
    }
}
